﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dm.Model.V20170622;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Syspetro.Core;
using Syspetro.Core.HttpService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Syspetro.Aliyun
{
    public class EmailService : IEmailService
    {
        private readonly EmailOptions emailOptions;

        public EmailService()
        {
            emailOptions = new EmailOptions();
            InternalApp.Configuration.Bind("EmailOptions", emailOptions);
            if (emailOptions == null) emailOptions = new EmailOptions();
        }

        public int GetPerLimit()
        {
            return emailOptions.PerLimit;
        }

        public int GetPerLimitHours()
        {
            return emailOptions.PerLimitHours;
        }

        public int GetValidTime()
        {
            return emailOptions.ValidTime;
        }

        public (bool status, string error) Notify(string email, string content, string code = "888888", string subject = "邮箱验证")
        {
            if (!emailOptions.Enabled) return (false, "服务暂停");

            var profile = DefaultProfile.GetProfile(emailOptions.Region, emailOptions.AppKey, emailOptions.AppSecret);

            switch (emailOptions.Region)
            {
                case "ap-southeast-1":
                    profile.AddEndpoint("ap-southeast-1", "ap-southeast-1", "Dm", "dm.ap-southeast-1.aliyuncs.com");
                    break;
                case "ap-southeast-2":
                    profile.AddEndpoint("ap-southeast-2", "ap-southeast-2", "Dm", "dm.ap-southeast-2.aliyuncs.com");
                    break;
            }

            var client = new DefaultAcsClient(profile);
            var request = new SingleSendMailRequest();
            if (string.IsNullOrEmpty(content))
                content = string.Format(emailOptions.Templete, code, emailOptions.ValidTime);

            try
            {
                request.AccountName = emailOptions.Sender;
                request.FromAlias = emailOptions.Alia;
                request.AddressType = 1;
                request.TagName = emailOptions.Tag;
                request.ReplyToAddress = false;
                request.ToAddress = email;
                request.Subject = subject;
                request.HtmlBody = content;
                var response = client.GetAcsResponse(request);

                return (true, $"发送成功,EnvId:{response.EnvId},Status:{response.HttpResponse.Status}");
            }
            catch (ServerException e)
            {
                return (false, e.ErrorMessage);
            }
            catch (ClientException e)
            {
                return (false, e.ErrorMessage);
            }
            catch (Exception e)
            {
                return (false, e.Message);
            }
        }
    }
}
